﻿
//依赖项：
//mfcvcr100.dll;kernel32.dll


#include "../common/serialPort.h"
#include "../common/read_dev_head.h"

serialPort com;
static int delaytime;


//dll入口
BOOL APIENTRY DllMain( HANDLE hModule,
	DWORD  ul_reason_for_call, 
	LPVOID lpReserved
	)
{
	switch( ul_reason_for_call )
	{
	case DLL_PROCESS_ATTACH:break;
	case DLL_THREAD_ATTACH:break;
	case DLL_THREAD_DETACH:break;
	case DLL_PROCESS_DETACH:
		com.CloseCom();
	}

	delaytime=20;
	return TRUE;
}


void removeCRLF(char*buf_,char*dst)
{
	size_t size=strlen(buf_);

	memset(dst,0,sizeof(dst));
	size_t index=0;
	for(size_t ki=0;ki<size;ki++)
	{
		if(buf_[ki]=='\r' || buf_[ki]=='\n')
			continue;
		dst[index++]=buf_[ki];

	}
}

bool getx(char*dat) 
{
	send("?1\r");
	//Sleep(500);
	waitcom();
	Sleep(delaytime);
	char buf[200];
	bool res=read(buf);
	if(!res)
		return false;
	removeCRLF(buf,dat);
	return true;
}
bool gety(char*dat) 
{
	if(!send("?2\r"))
	{
		return false;
	}
	waitcom();
	Sleep(delaytime);
	char buf[200];
	bool res=read(buf);
	if(!res)
		return false;
	removeCRLF(buf,dat);
	return true;
}
bool getall(char*dat) 
{
	if(!send("?1;?2\r"))
	{
		return false;
	}
	waitcom();
	Sleep(delaytime);
	char buf[200];
	bool res=read(buf);
	if(!res)
		return false;
	removeCRLF(buf,dat);
	return true;
}


extern "C" __declspec(dllexport) void setupReaderEngine(readerInterface& dest)
{
	dest.openCom=openCom;
	dest.closeCom=closeCom;
		//com.SetMask(EV_RXCHAR);

	dest.pgetx=getx;
	dest.pgety=gety;
	dest.pgetall=getall;
}


